﻿using B.MESUI.Login.BasicData_UI.BasicData;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace B.MESUI.Login.BasicData_UI.Material
{
    public partial class EditMaterialForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void EditMaterialEventHandler();
        public event EditMaterialEventHandler EditMaterialEvent;
        private readonly UpdateMaterialDTO updateMaterialDTO;
        public EditMaterialForm(UpdateMaterialDTO updateMaterialDTO)
        {
            InitializeComponent();
            this.updateMaterialDTO = updateMaterialDTO;
            WLIdtextEdit1.Text = updateMaterialDTO.Id;
            WLtextEdit2.Text = updateMaterialDTO.MaterialCode;
            WLtextEdit1.Text = updateMaterialDTO.MaterialName;
            WLtextEdit3.Text = updateMaterialDTO.Spec;
            WLtextEdit4.Text = updateMaterialDTO.Remark;
            WLcomboBoxEdit1.Text = updateMaterialDTO.Unit;
        }

        private async void WLsimpleButton1_Click(object sender, EventArgs e)
        {
            string url = $"http://localhost:5198/api/Material/UpdateMaterial";
            UpdateMaterialDTO data = new UpdateMaterialDTO()
            {
                Id = updateMaterialDTO.Id,
                MaterialCode = WLtextEdit2.Text,
                MaterialName = WLtextEdit1.Text,
                Spec = WLtextEdit3.Text,
                Unit = WLcomboBoxEdit1.Text,
                Remark = WLtextEdit4.Text
            };
            string jsonData = JsonConvert.SerializeObject(data);
            string result = await BaseMethod.MethodsURL("put", url, jsonData);
            if (result != null)
            {
                MessageBox.Show("修改成功");
                EditMaterialEvent.Invoke();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }
    }
}